<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="项目信息" prop="tablename">
        <el-select v-model="queryParams.tablename" placeholder="请选择项目">
          <el-option
            v-for="dict in formOptionList"
            :key="dict.fromTableName"
            :label="dict.fromname"
            :value="dict.fromTableName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="辅助信息" prop="auditAuxiliaryInfo">
        <el-input
          v-model="queryParams.auditAuxiliaryInfo"
          placeholder="请输入辅助信息"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号码" prop="zjhm">
        <el-input
          v-model="queryParams.zjhm"
          placeholder="请输入证件号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['grant:auditauxiliary:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['grant:auditauxiliary:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['grant:auditauxiliary:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['grant:auditauxiliary:export']"
        >导出</el-button>
      </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            @click="handleImport"
          >导入辅助信息</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="auditauxiliaryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目信息" align="center" prop="tablename"  :formatter="tablenameFormat"/>
      <el-table-column label="辅助信息" align="center" prop="auditAuxiliaryInfo" />
      <el-table-column label="证件号码" align="center" prop="zjhm" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['grant:auditauxiliary:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['grant:auditauxiliary:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改审核辅助信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目信息" prop="tablename">
          <el-select v-model="form.tablename" placeholder="请选择项目">
            <el-option
              v-for="dict in formOptionList"
              :key="dict.fromTableName"
              :label="dict.fromname"
              :value="dict.fromTableName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="辅助信息" prop="auditAuxiliaryInfo">
          <el-input v-model="form.auditAuxiliaryInfo" placeholder="请输入辅助信息" />
        </el-form-item>
        <el-form-item label="证件号码" prop="zjhm">
          <el-input v-model="form.zjhm" placeholder="请输入证件号码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 批量导入详情对话框 -->
    <el-dialog :title="title" :visible.sync="importOpen" width="500px" append-to-body>
      <el-form ref="form" :model="importform" :rules="rules" label-width="80px">
        <el-form-item label="备注:" prop="remark1">
          <el-select v-model="importform.tablename" placeholder="请选择项目">
            <el-option
              v-for="dict in formOptionList"
              :key="dict.fromTableName"
              :label="dict.fromname"
              :value="dict.fromTableName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="上传附件:" prop="voucher">
          <el-upload action="#"
                     :http-request="requestUpload"
                     :limit="1"
                     :show-file-list="true"
                     :before-upload="beforeUpload"
                     :file-list="files">
            <el-button size="small">
              上传附件
              <i class="el-icon-upload el-icon--right"></i>
            </el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div>只能上传xls,xlsx文件,文件大小不超过5M,
        时间格式采用文本格式
        <!--        <el-link type="primary" style="font-size:12px" @click="importTemplate">下载模板</el-link>-->
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary"  @click="submitImportForm">确 定</el-button>
        <el-button @click="importFormCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAuditauxiliary, getAuditauxiliary, delAuditauxiliary, addAuditauxiliary, updateAuditauxiliary, exportAuditauxiliary ,importAuditauxiliaryData} from "@/api/grant/auditauxiliary";
import { listTappfromAll} from "@/api/grant/tappfrom";

export default {
  name: "Auditauxiliary",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 审核辅助信息表格数据
      auditauxiliaryList: [],
      onlineFile: undefined,
      importform:{},
      importOpen: false,
      //项目选择列表
      formOptionList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tablename: undefined,
        auditAuxiliaryInfo: undefined,
        zjhm: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    listTappfromAll().then(res=>{
      this.formOptionList = res.data;
    })
  },
  methods: {
    /** 查询审核辅助信息列表 */
    getList() {
      this.loading = true;
      listAuditauxiliary(this.queryParams).then(response => {
        this.auditauxiliaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        tablename: undefined,
        auditAuxiliaryInfo: undefined,
        zjhm: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审核辅助信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAuditauxiliary(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改审核辅助信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateAuditauxiliary(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addAuditauxiliary(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除审核辅助信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAuditauxiliary(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有审核辅助信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAuditauxiliary(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    tablenameFormat(row,column){
      var actions = [];
      Object.keys(this.formOptionList).map((key) => {
        if(this.formOptionList[key].fromTableName==row.tablename){
          actions.push(this.formOptionList[key].fromname);
        }
      })
      return actions.join('');
    }
    ,
    handleImport(){
      this.importOpen=true;
    },
    importFormCancel(){
      this.importOpen = false;
    },
    submitImportForm(){
      if(this.importform.tablename === undefined){
        this.msgError('请选择项目')
        return
      }
      if(this.onlineFile === undefined){
        this.msgError('请上传附件')
        return
      }
      //先上传图片获取图片路径
      let formData = new FormData();
      formData.append("onlineFile", this.onlineFile);
      formData.append("tablename", this.importform.tablename);
      this.loading = true;
      this.busy = true;
      importAuditauxiliaryData(formData).then(response => {
        if (response.code === 200) {
          this.msgSuccess("上传成功");
          this.open = false;
          this.getList();
        }else{
          this.msgError(response.msg);
          this.getList();
        }
        this.loading = false;
        this.busy = false;
      });
    },
    // 覆盖默认的上传行为
    requestUpload() {
    },
    // 上传预处理
    beforeUpload(file) {
      if(file.size/(1024 * 1024) > 5){
        this.msgError("文件大小超过5M，请分批进行上传");
        this.files = [];
        return;
      }
      if (file.type.indexOf("sheet") === -1) {
        this.files = [];
        this.msgError("文件格式错误，请上传Excel类型,如：xls，xlsx后缀的文件。");
      }else {
        this.onlineFile = file;
      }
    }
  }
};
</script>
